package com.bnz.servlet;

import com.alibaba.fastjson.JSON;
import com.bnz.pojo.PageResult;
import com.bnz.pojo.R;
import com.bnz.pojo.Student;
import com.bnz.service.StudentService;
import com.bnz.service.impl.StudentServiceImpl;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.util.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/student")
public class StudentServlet extends HttpServlet {

    private StudentService studentService;

    @Override
    public void init() throws ServletException {
        //在service里得到spring容器
       WebApplicationContext wac=WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
       //得到studentService
        studentService=wac.getBean(StudentService.class);

    }

    //根据请求参数调用方法
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("UTF-8");
        //得到请求参数
        String cmd=req.getParameter("cmd");
        //根据参数调用方法
        if(StringUtils.isNotBlank(cmd)){
            if("list".equals(cmd)){//查询所有学生
                list(req,resp);
            }
            if("findByPage".equals(cmd)){
                findByPage(req,resp);
            }
            if("search".equals(cmd)){
                search(req,resp);
            }
        }

    }

    private void search(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        //得到当前页和每页大小
        int page=new Integer(req.getParameter("page"));
        int pageSize=new Integer(req.getParameter("pageSize"));
        //转化
        String stud=req.getParameter("stud");
        Student student=null;
        if(StringUtils.isNotBlank(stud)){
            student=JSON.parseObject(stud,Student.class);
        }
        //开始分页
        PageResult<Student> pr=studentService.search(page,pageSize,student);
        //提交数据到后台
        resp.getWriter().println(JSON.toJSONString(R.ok().data("pr",pr)));
        //刷新页面
        resp.getWriter().flush();

    }

    private void findByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //得到当前页和每页大小
        int page=new Integer(req.getParameter("page"));
        int pageSize=new Integer(req.getParameter("pageSize"));
        //开始分页
        PageResult<Student> pr=studentService.findByPage(page,pageSize);


        //提交数据到后台
        resp.getWriter().println(JSON.toJSONString(R.ok().data("pr",pr)));
        //刷新页面
        resp.getWriter().flush();


    }

    //列表学生
    private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //查询所有学生
        List<Student> students=studentService.findAll();

        R data= R.ok().data("data",students);
        //将学生转换成json输出
        resp.getWriter().println(JSON.toJSONString(data));
        //刷新页面
        resp.getWriter().flush();


    }
}
